﻿using Andy.Web.BinhLuanServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Andy.Web
{
    public partial class TrangChu : VdcInc.vdcAJAXPage
    {
        static int recodperpage = 10;
        static int Pagesize = 5;
        static string func = "paging";

        static int recodperpage_Left = 10;
        static int Pagesize_Left = 5;
        static string func_Left = "pagingLeft";

        static int recodperpage_sp = 12;
        static int Pagesize_sp = 5;
        static string func_sp = "paging_sp";

        protected string IsColor()
        {
            using (var chService = new CauHinhServiceReference.CauHinhServiceClient())
            {
                var ch = chService.ChiTietCauHinh();
                if (ch != null)
                {
                    return "background-color:" + ch.Text5;
                }
            }
            return "background-color:#FFFFFF";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                plHolder.Controls.Add(LoadControl("~/slide3.ascx"));

                loadSanPham(1);

                loadBinhLuanLeft(1);

                loadCauHinhTrangChu();

                
            }

        }
        public void loadCauHinhTrangChu()
        {
            using (var chService = new CauHinhServiceReference.CauHinhServiceClient())
            {
                var ch = chService.ChiTietCauHinh();
                if (ch != null)
                {
                    ltrTitle.Text = ch.Text2;
                    ltrTitleBanner.Text = ch.Text1;
                    ltrTitleChiTiet.Text = ch.Text3;

                    imgMuaHang.Src = ch.Image1;
                    Img_banner.Src = ch.Image2;
                }
            }
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var lstLoaiSanPham = spService.DanhSachLoaiSanPham();
                RptMenuLoaiSanPham.DataSource = lstLoaiSanPham;
                RptMenuLoaiSanPham.DataBind();
            }
        }
        public void loadSanPham(int currPage)
        {
            long idloai=0;
            if (Request.QueryString["loai"] != "")
            {
                int num;
                bool isNum = Int32.TryParse(Request.QueryString["loai"], out num);

                if (isNum)
                {
                    idloai = Convert.ToInt64(Request.QueryString["loai"]);
                }
            }

            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var lst = spService.DanhSachSanPham(Convert.ToInt64(idloai), currPage, recodperpage_sp, Pagesize_sp, func_sp);
                if (lst != null)
                {
                    if (lst.lstSanPham != null)
                    {
                        lvDanhSach.DataSource = lst.lstSanPham;
                        lvDanhSach.DataBind();

                        ltrPhanTrangSP.Text = lst.PhanTrangSP;

                        UpdatePanel("cbpDanhSachSanPham");
                        UpdatePanel("cbpPhanTrangSP");
                    }
                }

            }
        }
        public void loadBinhLuanLeft(int currPage)
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var listBinhLuan = spService.DanhSachBinhLuan(currPage, recodperpage_Left, Pagesize_Left, func_Left);
                if (lstBinhLuan != null)
                {
                    if (listBinhLuan.lstBinhLuan != null)
                    {
                        lstBinhLuan.DataSource = listBinhLuan.lstBinhLuan;
                        lstBinhLuan.DataBind();

                        ltrPhanTrang_left.Text = listBinhLuan.PhanTrangBL;

                        UpdatePanel("cpbBinhLuan_left");
                        UpdatePanel("cbpPhanTrang_left");
                    }
                }

            }
        }
        public void loadDialogChiTiet(string idsanpham)
        {
            if (!string.IsNullOrEmpty(idsanpham))
            {
                loadBinhLuan(idsanpham, 1);

            }
        }
        public string InsertBinhLuan(string txtNoiDung, string idsanpham, string txtCapcha, string txtHashCapcha)
        {
            if (rpHash(txtCapcha) == txtHashCapcha)
            {
                Andy.Web.BinhLuanServiceReference.BinhLuan bl = new BinhLuanServiceReference.BinhLuan();
                bl.NoiDung = txtNoiDung;
                bl.SanPhamId = Convert.ToInt64(idsanpham);
                bl.TrangThai = 0;
                using (var spService = new BinhLuanServiceReference.BinhLuanServiceClient())
                {
                    var result = spService.CapNhatBinhLuan(bl);
                    loadBinhLuan(idsanpham, 1);
                    UpdatePanel("cbpAnhChinh");
                    UpdatePanel("cbpBinhluan");
                    return result;
                }
            }
            else
            {
                return "Sai mã bảo vệ";
            }

        }
        public string InsertTraLoi(string txtNoiDung, string idsanpham, string idbinhluancha)
        {
            Andy.Web.BinhLuanServiceReference.BinhLuan bl = new BinhLuanServiceReference.BinhLuan();
            bl.NoiDung = txtNoiDung;
            bl.SanPhamId = Convert.ToInt64(idsanpham);
            bl.TrangThai = 0;
            bl.IdBinhLuanCha = Convert.ToInt64(idbinhluancha);
            using (var spService = new BinhLuanServiceReference.BinhLuanServiceClient())
            {
                var result = spService.CapNhatBinhLuan(bl);
                loadBinhLuan(idsanpham, 1);
                UpdatePanel("cbpAnhChinh");
                UpdatePanel("cbpBinhluan");
                return result;
            }
        }

        public string vXoabinhLuan(string idsanpham, string strBinhLuanId)
        {
            using (var spService = new BinhLuanServiceReference.BinhLuanServiceClient())
            {
                if (!string.IsNullOrEmpty(idsanpham))
                {
                    var bl = new BinhLuan();
                    bl.Id = Convert.ToInt64(strBinhLuanId);
                    var result = spService.CapNhatBinhLuan(bl);

                    loadBinhLuan(idsanpham, 1);
                    UpdatePanel("cbpAnhChinh");
                    UpdatePanel("cbpBinhluan");
                    return "Xóa " + result;

                }
                return "Xóa thất bại";
            }
        }
        public void loadBinhLuan(string idsanpham, int currPage)
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                if (!string.IsNullOrEmpty(idsanpham))
                {
                    var sanpham = spService.ChiTietSanPham(Convert.ToInt64(idsanpham), currPage, recodperpage, Pagesize,func);
                    if (sanpham != null)
                    {
                        imgChinh.Src =  sanpham.Anh;
                        TenSanPham.InnerText = sanpham.Ten;
                        GiaSanPham.InnerText = String.Format("{0:0,0}", sanpham.Gia);
                        UpdatePanel("cbpTen");
                        UpdatePanel("cbpGia");
                        UpdatePanel("cbpAnhChinh");

                        if (sanpham.dsBinhLuan != null)
                        {
                            lvBinhLuan.DataSource = sanpham.dsBinhLuan.lstBinhLuan;
                            lvBinhLuan.DataBind();

                            ltlPhanTrang.Text = sanpham.dsBinhLuan.PhanTrangBL;

                            UpdatePanel("cbpBinhluan");
                            UpdatePanel("cbpPTBinhLuan");
                        }
                        else
                        {
                            lvBinhLuan.DataSource = null;
                            lvBinhLuan.DataBind();

                            ltlPhanTrang.Text = "";

                            UpdatePanel("cbpBinhluan");
                            UpdatePanel("cbpPTBinhLuan");
                        }
                    }
                }

            }

        }

        private string rpHash(string value)
        {
            int hash = 5381;
            value = value.ToUpper();
            for (int i = 0; i < value.Length; i++)
            {
                hash = ((hash << 5) + hash) + value[i];
            }
            return hash.ToString();
        }
    }
}